package indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.impl;

import com.alibaba.fastjson2.JSONObject;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.IAuthNodeDbService;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.IAuthNodeRelevantDao;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.IDomainDbService;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.IRelUserDepDbService;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.po.AuthNodeEntity;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.po.DomainEntity;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.po.RelUserAuthEntity;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.BaseAuthNodeEntity;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.BaseDomainEntity;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.BaseRelUserAuthEntity;
import indi.zhifa.recipe.bailan.framework.common.entity.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Component
public class AuthNodeRelevantDaoImpl implements IAuthNodeRelevantDao {

    private final IDomainDbService mDomainDbService;
    private final IAuthNodeDbService mDepartmentDbService;
    private final IRelUserDepDbService mRelUserDepDbService;

    @Override
    public DomainEntity domain_generate() {
        return new DomainEntity();
    }

    @Override
    public DomainEntity domain_load(JSONObject pCfg) {
        DomainEntity domainEntity = pCfg.to(DomainEntity.class);
        domainEntity.createInit();
        return domainEntity;
    }

    @Override
    public DomainEntity domain_check(Long pId) {
        return mDomainDbService.check(pId);
    }

    @Override
    public Long domain_findByCode(String pCode) {
        Long id = mDomainDbService.findByCode(pCode);
        return id;
    }

    @Override
    public List<BaseDomainEntity> domain_list() {
        List<DomainEntity> domainEntityList = mDomainDbService.list();
        return new ArrayList<>(domainEntityList);
    }

    protected DomainEntity checkDomain(BaseDomainEntity pBaseDomainEntity){
        if(pBaseDomainEntity instanceof DomainEntity){
            return (DomainEntity)pBaseDomainEntity;
        }
        throw new ServiceException("pBaseDomainEntity需要是子类");
    }

    protected AuthNodeEntity checkDepartmentEntity(BaseAuthNodeEntity pBaseAuthNodeEntity){
        if(pBaseAuthNodeEntity instanceof AuthNodeEntity){
            return (AuthNodeEntity) pBaseAuthNodeEntity;
        }
        throw new ServiceException("pBaseDepartmentEntity需要是子类");
    }

    protected RelUserAuthEntity checkRelUserDepEntity(BaseRelUserAuthEntity pBaseRelUserAuthEntity){
        if(pBaseRelUserAuthEntity instanceof RelUserAuthEntity){
            return (RelUserAuthEntity) pBaseRelUserAuthEntity;
        }
        throw new ServiceException("pBaseRelUserDepEntity需要是子类");
    }

    @Override
    public DomainEntity authNode_save(BaseDomainEntity pBaseDomainEntity) {
        DomainEntity domainEntity = checkDomain(pBaseDomainEntity);
        mDomainDbService.savePull(pBaseDomainEntity.getId(),domainEntity);
        return domainEntity;
    }

    @Override
    public AuthNodeEntity authNode_generate() {
        return new AuthNodeEntity();
    }

    @Override
    public AuthNodeEntity authNode_load(JSONObject pCfg) {
        AuthNodeEntity authNodeEntity = pCfg.to(AuthNodeEntity.class);
        authNodeEntity.createInit();
        return authNodeEntity;
    }

    @Override
    public Long authNode_findByDomainAndCode(Long pDomainId, String pCode) {
        return mDepartmentDbService.findByDomainIdAndCode(pDomainId,pCode);
    }

    @Override
    public AuthNodeEntity authNode_check(Long pId) {
        return mDepartmentDbService.check(pId);
    }

    @Override
    public AuthNodeEntity authNode_save(BaseAuthNodeEntity pBaseAuthNodeEntity) {
        AuthNodeEntity authNodeEntity = checkDepartmentEntity(pBaseAuthNodeEntity);
        mDepartmentDbService.savePull(pBaseAuthNodeEntity.getId(), authNodeEntity);
        return authNodeEntity;
    }

    @Override
    public AuthNodeEntity authNode_update(Long pId, BaseAuthNodeEntity pBaseAuthNodeEntity) {
        AuthNodeEntity authNodeEntity = checkDepartmentEntity(pBaseAuthNodeEntity);
        mDepartmentDbService.updatePull(pId, authNodeEntity);
        return authNodeEntity;
    }

    @Override
    public List<BaseAuthNodeEntity> authNode_listByDomainId(Long pDomainId) {
        List<AuthNodeEntity> authNodeEntityList = mDepartmentDbService.listByDomainId(pDomainId);
        return new ArrayList<BaseAuthNodeEntity>(authNodeEntityList);
    }

    @Override
    public List<BaseAuthNodeEntity> authNode_listByParentId(Long pParentId) {
        List<AuthNodeEntity> children = mDepartmentDbService.listByParentId(pParentId);
        List<BaseAuthNodeEntity> rtn = new ArrayList<>(children);
        return rtn;
    }

    @Override
    public List<BaseAuthNodeEntity> authNode_listDescendants(String pCascade) {
        List<AuthNodeEntity> descendants = mDepartmentDbService.listByBeginWith(pCascade);
        List<BaseAuthNodeEntity> rtn = new ArrayList<>(descendants);
        return rtn;
    }

    @Override
    public AuthNodeEntity authNode_delete(Long pId) {
        AuthNodeEntity baseAuthNodeEntity = mDepartmentDbService.deleteById(pId);
        return baseAuthNodeEntity;
    }

    @Override
    public RelUserAuthEntity relUserAuth_generate() {
        return new RelUserAuthEntity();
    }

    @Override
    public List<BaseRelUserAuthEntity> relUserAuth_listByUserId(Long pId) {
        List<RelUserAuthEntity> relUserDepEntities = mRelUserDepDbService.listByUserId(pId);
        List<BaseRelUserAuthEntity> baseRelUserAuthEntityList = new ArrayList<>(relUserDepEntities);
        return baseRelUserAuthEntityList;
    }

    @Override
    public List<BaseRelUserAuthEntity> relUserAuth_saveBatch(Long pUserId, List<BaseRelUserAuthEntity> pBaseRelUserAuthEntityList) {
        List<RelUserAuthEntity> relUserAuthEntityList = new ArrayList<RelUserAuthEntity>();
        for(BaseRelUserAuthEntity baseRelUserAuthEntity : pBaseRelUserAuthEntityList){
            relUserAuthEntityList.add(checkRelUserDepEntity(baseRelUserAuthEntity));
        }
        mRelUserDepDbService.saveBatch(pUserId, relUserAuthEntityList);
        return pBaseRelUserAuthEntityList;
    }

    @Override
    public void relUserAuth_deleteByUserId(Long pUserId) {
        mRelUserDepDbService.deleteByUserId(pUserId);
    }



}
